//
//  SongMenuCollectionViewCell.m
//  NewMusic
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SongMenuCollectionViewCell.h"
#import "CXYSongMenuCollectionViewCell.h"
#import "CustomerNetworking.h"
#import "SongsMenuCollectionReusableView.h"
#import <MJRefresh.h>

#define kWidth self.contentView.frame.size.width
#define kHeight self.contentView.frame.size.height

@interface SongMenuCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SongsMenuCollectionReusableViewDelegate>
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, copy) NSString *CXYString;
@property (nonatomic, copy) NSString *buttonString;
@property (nonatomic, copy) NSString * andString;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, assign) BOOL isOk;
@end

@implementation SongMenuCollectionViewCell

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:@"SongsTitle"];
    [_songMuneCollectionView release];
    [_CXYString release];
    [_array release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _isOk = YES;
        //消息通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"SongsMenu" object:nil];
        _number = 1;
        self.array = [NSMutableArray array];
        self.CXYString = @"最热";
        self.buttonString = @"";
        self.andString = @"";
        
        [self customer];
        [self buildCollectionView];
    }
    return self;
}

- (void)receiveNotification:(NSNotification *)notification
{
    _isOk = YES;
    _buttonString = notification.userInfo[@"SongsTitle"];
    [self customer];
}

- (void)customer
{
    if ([_buttonString isEqual:@""]) {
        _andString = @"";
    }else if ([_buttonString isEqual:@"全部"]) {
        _andString = @"";
        _buttonString = @"";
    }else{
        _andString = @"%7C";
    }

    NSLog(@"🍎%@", _andString);
    
    NSString *firstString = @"http://so.ard.iyyin.com/s/songlist?q=tag:";
    NSString *fifthString = [NSString stringWithFormat:@"&page=%ld&size=10&app=ttpod&v=v8.1.1.2015110616&user_id=1&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.88255&longtitude=121.5398", _number];
    NSString *fourthString = [_CXYString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *sixString = [_buttonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *string = [NSString stringWithFormat:@"%@%@%@%@%@", firstString, fourthString, _andString, sixString, fifthString];


    
    [CustomerNetworking customerConnectWithURL:string andParameters:nil andBlock:^(id response) {
        NSDictionary *dictionary = response;
        NSArray *array = [dictionary objectForKey:@"data"];
        if (_isOk == YES) {
            [self.array removeAllObjects];
        }else{
        }
        for (NSDictionary *dic in array) {
            SongMenuModel *songMenuModel = [SongMenuModel songMenuModelWithDictionary:dic];
            [self.array addObject:songMenuModel];
        }
        [self.songMuneCollectionView reloadData];
        [self.songMuneCollectionView.footer endRefreshing];
    }];
}

- (void)buildCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.songMuneCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, self.contentView.frame.size.height) collectionViewLayout:flowLayout];
    self.songMuneCollectionView.backgroundColor = [UIColor clearColor];
    self.songMuneCollectionView.contentOffset = CGPointMake(0, -200);
    self.songMuneCollectionView.delegate = self;
    self.songMuneCollectionView.dataSource = self;
    
    //加载
    self.songMuneCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(oldData)];
    //注册collectioncell
    [self.songMuneCollectionView registerClass:[CXYSongMenuCollectionViewCell class] forCellWithReuseIdentifier:@"cxySongMenuCollectionViewCell"];

    //注册头视图
    [self.songMuneCollectionView registerClass:[SongsMenuCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"songsMenuCollectionReusableView"];
    [self.contentView addSubview:_songMuneCollectionView];
    [_songMuneCollectionView release];
}
//刷新点击事件
- (void)oldData
{
    _isOk = NO;
    _number += 1;
    [self customer];
}


//代理点击事件
- (void)custumerButton:(UIButton *)button
{
    _isOk = YES;
    if (button.tag == 1011) {
        _CXYString = @"最新";
        [self customer];
    }else if (button.tag == 1010) {
        _CXYString = @"最热";
        [self customer];
    }
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        SongsMenuCollectionReusableView *songReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"songsMenuCollectionReusableView" forIndexPath:indexPath];
        songReusableView.delegate = self;
        return songReusableView;
    }
    return nil;
}

//全部歌单点击事件
- (void)CXYButton:(UIButton *)sender
{
    [self.delegate CXYPushButton:sender];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(375, 40);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%ld", _array.count);
    return self.array.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXYSongMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cxySongMenuCollectionViewCell" forIndexPath:indexPath];
    cell.songMenuModel = [self.array objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return (kWidth - 330) / 3;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return (kWidth - 330) / 3;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, (kWidth - 330) / 3, 0, (kWidth - 330) / 3);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate CXYPushSongsMenuController:[[self.array objectAtIndex:indexPath.row] ID]];
    NSLog(@"section = %ld  row = %ld", indexPath.section, indexPath.row);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(165, 200);
}


@end
